package cn.virens.web.controller.manage.sys;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.virens.common.RequestUtil;
import cn.virens.components.page.PageParam;
import cn.virens.components.page.PageResult;
import cn.virens.database.mysql.model.sys.SysUser;
import cn.virens.database.service.sys.SysRoleService;
import cn.virens.database.service.sys.SysUserService;
import cn.virens.web.common.BaseTableController;

/**
 * 用户管理 - sys_user
 *
 * @author Virens
 * @since  2018-08-25 15:39:30
 */
@Controller
@RequestMapping("/manage/sys/user")
public class SysUserContorller extends BaseTableController<SysUser, SysUserService> {
	private @Autowired SysRoleService mSysRoleService;

	public SysUserContorller() {
		super("/manage/sys/user", "sys:user");
	}

	@Override
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		String answer = super.index(request, response, model);

		model.addAttribute("roles", mSysRoleService.selectAll(null));

		return answer;
	}

	@RequestMapping("view.jspx")
	public String viewJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("record", mBaseService.selectOne(RequestUtil.getValueStr(request, "id")));

		return returnUrl("view");
	}

	@RequestMapping("create.jspx")
	public String createJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("roles", mSysRoleService.selectAll(null));

		return returnUrl("create");
	}

	@RequestMapping("update.jspx")
	public String updateJspx(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("record", mBaseService.selectOne(RequestUtil.getValueStr(request, "id")));
		model.addAttribute("roles", mSysRoleService.selectAll(null));

		return returnUrl("update");
	}

	/**
	 * 复写一次，保证密码被MD5加密
	 */
	@Override
	protected Model createHandle(HttpServletRequest request, Model model, SysUser record) throws Exception {
		record.setPassword(formatPssword(record));

		return super.createHandle(request, model, record);
	}

	@Override
	protected Serializable toId(String id) {
		return id;
	}

	/**
	 * 复写一次，保证密码被MD5加密
	 */
	@Override
	protected Model updateHandle(HttpServletRequest request, Model model, SysUser record) throws Exception {
		record.setPassword(formatPssword(record));

		return super.updateHandle(request, model, record);
	}

	@Override
	protected PageResult<?> listHandle(HttpServletRequest request, PageParam page) throws Exception {
		page.addParam("account", RequestUtil.getValueStr(request, "account", null));
		page.addParam("name", RequestUtil.getValueStr(request, "name", null));
		page.addParam("role", RequestUtil.getValueInt(request, "role", null));

		return super.listHandle(request, page);
	}

	private static String formatPssword(SysUser record) {
		return StrUtil.isEmpty(record.getPassword()) ? null : DigestUtil.md5Hex(record.getPassword());
	}
}
